package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_REDUCE_DART_TIME, desc = "镖车加速")
public class BeautyEscortReduceDartTimeCmd extends AbstractCommand {

    int cartId;
    int num;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortReduceDartTimeReqMsg reqMsg = BeautyEscortProto.BeautyEscortReduceDartTimeReqMsg.parseFrom(packet.getBytes());
        cartId = reqMsg.getCartId();
        num = reqMsg.getGoodsNum();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        //商会等级限制
        UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
        if(UnionConfigMgr.getUnionLv(unionInfo.getExperience()) < GameConfig.ESCORT_OPEN_UNION_LEVEL) {
            ret = GameErrorCode.E_BEAUTY_ESCORT_UNION_LEVEL_NO_ENOUGH;
            return;
        }
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(eItemId.BEAUTY_ESCORT_JI_XING_LING.getValue());
        if(goodsInfo == null){
            ret = GameErrorCode.E_GOODS_NO_EXIST;
            return;
        }
        //道具是否足够
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(eItemId.BEAUTY_ESCORT_JI_XING_LING.getValue() + "=" + num))){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }else{
            //预扣除
            player.getModule(PreReduceModule.class).preReduce(eItemId.BEAUTY_ESCORT_JI_XING_LING.getValue() + "=" + num);
        }
        BigInteger min = goodsInfo.getParamList().get(0);
        player.getModule(BeautyEscortModule.class).reduceEscortTime(min.intValue(), num, cartId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortReduceDartTimeRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortReduceDartTimeRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setCartId(cartId);
            player.sendPacket(Protocol.U_BEAUTY_ESCORT_REDUCE_DART_TIME, respMsg);
        }
    }
}
